library(kableExtra)
library(magrittr)
library(tidyverse)
library(texreg)


################################################################################
# Data Preparation
################################################################################
df <- as_tibble(read_csv("../data/regdata.csv"))

party_sharedict <- c(
    CDU_share = "Percentage of votes for CDU",
    SPD_share = "Percentage of votes for SPD",
    GRUENE_share = "Percentage of votes for Green Party",
    FDP_share = "Percentage of votes for FDP",
    DIE_LINKE_share = "Percentage of votes for Left Party",
    AfD_share = "Percentage of votes for AfD"
)
sumstatdict <- c(
    to_flooded = "Distance from flooding",
    flooded_area = "Share of area flooded",
    ratio_total = "Share of buildings affected",
    ratio_damaged = "Share of buildings damaged"
)

################################################################################
# Table A1
################################################################################

statnames <- c("", "N", "Mean", "St. Dev", "Min", "Pct(25)", "Pct(75)", "Max")
df %>%
    select(all_of(names(party_sharedict)), Land) %>%
    pivot_longer(!Land) %>%
    group_by(name, Land) %>%
    drop_na() %>%
    mutate(
        Land =
            ifelse(Land == 7, "Rhineland-Palatinate", "North Rhine-Westphalia")
    ) %>%
    summarize(
        n = n(),
        mean_val = mean(value),
        sd_val = sd(value),
        min_val = min(value),
        q25_val = quantile(value, probs = .25),
        q75_val = quantile(value, probs = .75),
        max_val = max(value)
    ) %>%
    ungroup() %>%
    arrange(desc(Land), match(name, names(party_sharedict))) %>%
    mutate(name = recode(name, !!!party_sharedict)) %>%
    select(!Land) %>%
    kbl(
        format = "latex",
        col.names = statnames,
        digits = 2,
        booktabs = TRUE,
        linesep = "",
        align = "lccccccc"
    ) %>%
    save_kable("../results/tables/table_a1.tex")

################################################################################
# Table A2
################################################################################

statnames <- c("", "N", "Mean", "St. Dev", "Min", "Pct(95)", "Pct(99)", "Max", "Median | > 0")
df %>%
    select(Verbandsgemeindeschluessel, all_of(names(sumstatdict)), Land) %>%
    distinct() %>%
    select(!Verbandsgemeindeschluessel) %>%
    pivot_longer(!Land) %>%
    drop_na() %>%
    group_by(name, Land) %>%
        mutate(
        Land =
            ifelse(Land == 7, "Rhineland-Palatinate", "North Rhine-Westphalia")
    ) %>%
    mutate(nonzero = value > 0) %>%
    summarize(
        n = n(),
        mean_val = mean(value),
        sd_val = sd(value),
        min_val = min(value),
        q95_val = quantile(value, probs = .95, na.rm = TRUE),
        q99_val = quantile(value, probs = .99, na.rm = TRUE),
        max_val = max(value),
        cond_median_val = median(value[nonzero])
    ) %>%
    ungroup() %>%
    arrange(desc(Land), match(name, names(sumstatdict))) %>%
    mutate(name = recode(name, !!!sumstatdict)) %>%
    select(!Land) %>%
    kbl(
        format = "latex",
        col.names = statnames,
        digits = 3,
        booktabs = TRUE,
        linesep = "",
        align = "lcccccccc"
    ) %>%
    save_kable("../results/tables/table_a2.tex")
